package cn.dengta.webapp.user.dao;

import java.util.Date;
import java.util.List;

import cn.dengta.common.model.TimeInterval;
import cn.dengta.webapp.base.dao.BaseDao;
import cn.dengta.webapp.user.entity.UserLoginStatistics;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.mapping.ResultSetType;

/**
 * 用户登录统计表 Mapper 接口
 *
 * @author zw
 * @since 2022-10-09
 */
public interface UserLoginStatisticsDao extends BaseDao<UserLoginStatistics> {

	@Update("UPDATE dt_user_login_statistics SET total_login_count = (total_login_count + 1), last_login_time = #{signTime}, " +
			"continuous_login_count = IF(#{continuous} = 1, continuous_login_count + 1, 1), login_times = login_times + 1  WHERE id = #{userId}")
	Integer updateLoginCount(@Param("userId") Long userId, @Param("signTime") Date signTime, @Param("continuous") int continuous);

	/** 更新总的登录次数 */
	@Update("UPDATE dt_user_login_statistics SET login_times = login_times + 1, last_login_time = #{signTime} WHERE id = #{userId}")
	int updateLoginTimes(@Param("userId") Long userId, @Param("signTime") Date signTime);

	@Select("SELECT id,continuous_login_count,gender FROM dt_user_login_statistics WHERE last_login_time BETWEEN #{lastLoginTime.finalBegin} AND #{lastLoginTime.finalEnd}")
	List<UserLoginStatistics> findContinuousLogin(@Param("lastLoginTime") TimeInterval lastLoginTime);

	@Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 1000)
	@ResultType(UserLoginStatistics.class)
	@Select("SELECT * FROM dt_user_login_statistics WHERE last_login_time > #{loginTime}")
	Cursor<UserLoginStatistics> findLoginCount(@Param("loginTime") Date loginTime);

}
